//
//  LPZWYMenuHeaderView.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPZWYMenuHeaderView.h"

@implementation LPZWYMenuHeaderView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles index:(NSInteger)index
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
       
        CGFloat Textwidth = [CalculateLabelHeight CalculateLabelWidthWithString:@"最新课程" font:16 * [UIScreen zj_textFontScale] height:20 * [UIScreen zj_screenHScale]].width;
        self.titleWidth = Textwidth;
        CGFloat  width = Textwidth + 20 * [UIScreen zj_screenWScale];
        
        
        for (int i = 0; i < titles.count; i ++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15 * [UIScreen zj_screenWScale] + width * i, 10 * [UIScreen zj_screenHScale], width, 20 * [UIScreen zj_screenHScale])];
            label.textColor = [UIColor textBlackColor];
            label.tag = i;
            label.userInteractionEnabled = YES;
            label.text = titles[i];
            label.font = LYPFONT(16);
            [self addSubview:label];
           
            self.underView.frame =CGRectMake(15 * [UIScreen zj_screenWScale] + width * index, 38 * [UIScreen zj_screenHScale], Textwidth, 1.5 * [UIScreen zj_screenHScale]);
            [self addSubview:self.underView];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel:)];
            [label addGestureRecognizer:tap];
            
           
        }
        
        self.moreButton = [UIButton new];
        [self.moreButton setTitle:@"更多" forState:UIControlStateNormal];
        [self.moreButton setImage:[UIImage imageNamed:@"icon_more_s"] forState:UIControlStateNormal];
        self.moreButton.titleLabel.font = LYPFONT(13);
        [self.moreButton setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        [self addSubview:self.moreButton];
        self.moreButton.sd_layout
        .rightSpaceToView(self, 15 * [UIScreen zj_screenWScale])
        .centerYEqualToView(self)
        .widthIs(60 * [UIScreen zj_screenWScale])
        .heightIs(20 * [UIScreen zj_screenHScale]);
        
        CGFloat imageWidth = self.moreButton.imageView.bounds.size.width;
        CGFloat labelWidth = self.moreButton.titleLabel.bounds.size.width;
        self.moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        self.moreButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
        
        [self.moreButton addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)tapLabel:(UITapGestureRecognizer *)tap{
    
    NSInteger index = tap.view.tag;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.underView.frame;
        frame.origin.x = 15 * [UIScreen zj_screenWScale] + (self.titleWidth + 20 * [UIScreen zj_screenWScale]) * index;
        self.underView.frame = frame;
    }];
    
    if (self.delegate) {
        [self.delegate LPZWYMenuHeaderView_clickTitleIndex:index];
    }
    
   
}

- (void)clickButton{
    if (self.delegate) {
        [self.delegate LPZWYMenuHeaderView_clickMore:self.section];
    }
}

- (UIView *)underView{
    if (_underView == nil) {
        _underView = [UIView new];
        _underView.backgroundColor = [UIColor themeColor];
        
    }
    return _underView;
}

@end
